package com.datamation.swadeshi.control.data;

import java.util.ArrayList;

import com.datamation.swadeshi.model.FDamDet;
import com.datamation.swadeshi.model.FTranDet;
import com.datamation.swadeshi.model.OrdDet;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;



public class FDamDetDS {
	private SQLiteDatabase dB;
	private DatabaseHelper dbHelper;
	Context context;
	private String TAG="swadeshi";
	
	
	public FDamDetDS (Context context)
	{
		this.context = context;
		dbHelper = new DatabaseHelper(context);
	}
	
	public void open() throws SQLException 
	{
		dB = dbHelper.getWritableDatabase();
	}
	
	
	
	@SuppressWarnings("static-access")
	public int createOrUpdateftrandet(ArrayList<FDamDet>  list) 
	{
		
		int serverdbID =0;
		
		if(dB == null){
			open();
		}else if(!dB.isOpen()){
			open();
		}
		Cursor cursor = null;
		
	try{
		
			
			
    for (FDamDet ftrandet : list) 
    {
		
			ContentValues values = new ContentValues();
			
			
			String selectQuery = "SELECT * FROM " + dbHelper.TABLE_FDAMDET 
					+ " WHERE " + dbHelper.FDAMDET_ID + " = '" +ftrandet.getFDAMDET_ID() + "'";	

			cursor = dB.rawQuery(selectQuery, null);
			
			//values.put(dbHelper.FTRANDET_ID, ftrandet.getFTRANDET_ID());
			values.put(dbHelper.FDAMDET_REFNO, ftrandet.getFDAMDET_REFNO());
			values.put(dbHelper.FDAMDET_TXNDATE, ftrandet.getFDAMDET_TXNDATE());
			values.put(dbHelper.FDAMDET_LOCCODE, ftrandet.getFDAMDET_LOCCODE());
			values.put(dbHelper.FDAMDET_TXNTYPE, ftrandet.getFDAMDET_TXNTYPE());
			values.put(dbHelper.FDAMDET_SEQNO, ftrandet.getFDAMDET_SEQNO());
			values.put(dbHelper.FDAMDET_ITEMCODE, ftrandet.getFDAMDET_ITEMCODE());
			values.put(dbHelper.FDAMDET_REACODE, ftrandet.getFDAMDET_REACODE());
			values.put(dbHelper.FDAMDET_REANAME, ftrandet.getFDAMDET_REANAME());
			values.put(dbHelper.FDAMDET_QTY, ftrandet.getFDAMDET_QTY());
			values.put(dbHelper.FDAMDET_AMT, ftrandet.getFDAMDET_AMT());
			values.put(dbHelper.FDAMDET_CASEQTY, ftrandet.getFDAMDET_CASEQTY());
			values.put(dbHelper.FDAMDET_PICEQTY, ftrandet.getFDAMDET_PICEQTY());
			values.put(dbHelper.REMQTY, ftrandet.getREMQTY());
			
			int cn = cursor.getCount();
			
			if(cn>0)
			{
			serverdbID = dB.update(dbHelper.TABLE_FDAMDET, values ,dbHelper.FDAMDET_ID + " =?",	
						new String[] { String.valueOf(ftrandet.getFDAMDET_ID())});
				
			}else 
			{
								
			serverdbID = (int) dB.insert(dbHelper.TABLE_FDAMDET, null, values);
			}
			
			}
		}catch (Exception e) {
		
			Log.v("Exception", e.toString());
	
		}finally {  
			if (cursor !=null) {
				cursor.close();
			}
			dB.close();
		}
		return serverdbID;
				
	}
	public ArrayList<String> gettrandet(String RefNo)
	{
		if (dB == null) {
			open();
		} else if (!dB.isOpen()) {
			open();
		}
		
		ArrayList<String> Itemname = new ArrayList<String>();

		String selectQuery = "select * from FDamDet where RefNo = '"+RefNo+"'";
		
		Cursor cursor = null;
		cursor = dB.rawQuery(selectQuery, null);
		
		while(cursor.moveToNext())
		{
		
			
			Itemname.add(cursor.getString(0));

		}
		
		return Itemname;
	}
	
	public ArrayList<FDamDet> getAllloadDetails(String refno) {
		if (dB == null) {
			open();
		} else if (!dB.isOpen()) {
			open();
		}
		
		ArrayList<FDamDet> list = new ArrayList<FDamDet>();
		
		String selectQuery = "select * from "+dbHelper.TABLE_FDAMDET +" WHERE "+dbHelper.FDAMDET_REFNO+"='"+refno+"'";
		
		Cursor cursor = dB.rawQuery(selectQuery, null);
		while(cursor.moveToNext()){
			
			FDamDet ftranset=new FDamDet();
		
			ftranset.setFDAMDET_ID(cursor.getString(cursor.getColumnIndex(dbHelper.FDAMDET_ID)));
			ftranset.setFDAMDET_REFNO(cursor.getString(cursor.getColumnIndex(dbHelper.FDAMDET_REFNO)));
			ftranset.setFDAMDET_TXNDATE(cursor.getString(cursor.getColumnIndex(dbHelper.FDAMDET_TXNDATE)));
			ftranset.setFDAMDET_LOCCODE(cursor.getString(cursor.getColumnIndex(dbHelper.FDAMDET_LOCCODE)));
			ftranset.setFDAMDET_TXNTYPE(cursor.getString(cursor.getColumnIndex(dbHelper.FDAMDET_TXNTYPE)));
			ftranset.setFDAMDET_SEQNO(cursor.getString(cursor.getColumnIndex(dbHelper.FDAMDET_SEQNO)));
			ftranset.setFDAMDET_ITEMCODE(cursor.getString(cursor.getColumnIndex(dbHelper.FDAMDET_ITEMCODE)));
			ftranset.setFDAMDET_REACODE(cursor.getString(cursor.getColumnIndex(dbHelper.FDAMDET_REACODE)));
			ftranset.setFDAMDET_QTY(cursor.getString(cursor.getColumnIndex(dbHelper.FDAMDET_QTY)));
			ftranset.setFDAMDET_AMT(cursor.getString(cursor.getColumnIndex(dbHelper.FDAMDET_AMT)));
			ftranset.setFDAMDET_CASEQTY(cursor.getString(cursor.getColumnIndex(dbHelper.FDAMDET_CASEQTY)));
			ftranset.setFDAMDET_PICEQTY(cursor.getString(cursor.getColumnIndex(dbHelper.FDAMDET_PICEQTY)));
			ftranset.setREMQTY(cursor.getString(cursor.getColumnIndex(dbHelper.REMQTY)));
			ftranset.setFDAMDET_REANAME(cursor.getString(cursor.getColumnIndex(dbHelper.FDAMDET_REANAME)));
							
			list.add(ftranset);
			
		}
		
		return list;
	}
	@SuppressWarnings("static-access")
	public int deleteOrdDetByID(String id){
		
		int count =0;
		
		if(dB == null){
			open();
		}else if(!dB.isOpen()){
			open();
		}
		Cursor cursor = null;
		try{
			
			cursor = dB.rawQuery("SELECT * FROM " + dbHelper.TABLE_FDAMDET +" WHERE "+dbHelper.FDAMDET_ID+"='"+id+"'", null);
			count =cursor.getCount();
			if(count>0){
				int success = dB.delete(dbHelper.TABLE_FDAMDET, dbHelper.FDAMDET_ID+"='"+id+"'", null);
				Log.v("FtranDet Deleted ", success+"");
			}
		}catch (Exception e){

			Log.v(TAG+" Exception", e.toString());
			
		}finally{  
			if (cursor !=null) {
				cursor.close();
			}
			dB.close();
		}
		
		return count;
		
	}
	@SuppressWarnings("static-access")
	public int DeltranDet(String refno) {
			
		int count =0;
			
		if(dB == null){
			open();
		}else if(!dB.isOpen()){
			open();
		}
		Cursor cursor = null;
			
		try{
						
			String selectQuery = "SELECT * FROM " + dbHelper.TABLE_FDAMDET + " WHERE " + dbHelper.FDAMDET_REFNO + " = '" +refno + "'";	
			cursor = dB.rawQuery(selectQuery, null);
			int cn = cursor.getCount();
				
			if(cn>0){
				count = dB.delete(dbHelper.TABLE_FDAMDET, dbHelper.FDAMDET_REFNO + " ='"+refno+"'", null);
				Log.v("Success Stauts", count+"");
			}
		}catch (Exception e) {
			Log.v(TAG+ " Exception", e.toString());
		}
			finally {  
				if (cursor !=null) {
					cursor.close();
					}
				dB.close();
			}
		return count;
					
	}
	
	@SuppressWarnings("static-access")
	public int restData(String refno) {
			
		int count =0;
			
		if(dB == null){
			open();
		}else if(!dB.isOpen()){
			open();
		}
		Cursor cursor = null;
			
		try{
						
			String selectQuery = "SELECT * FROM " + dbHelper.TABLE_FDAMDET + " WHERE " + dbHelper.FDAMDET_REFNO + " = '" +refno + "'";	
			cursor = dB.rawQuery(selectQuery, null);
			int cn = cursor.getCount();
				
			if(cn>0){
				count = dB.delete(dbHelper.TABLE_FDAMDET, dbHelper.FDAMDET_REFNO + " ='"+refno+"'", null);
				Log.v("Success Stauts", count+"");
			}
		}catch (Exception e) {
			Log.v(TAG+ " Exception", e.toString());
		}
			finally {  
				if (cursor !=null) {
					cursor.close();
					}
				dB.close();
			}
		return count;
					
	}
	
	
	public String getTotalReturns(String refno) {
		if (dB == null) {
			open();
		} else if (!dB.isOpen()) {
			open();
		}

		//String selectQuery = "SELECT * FROM " + dbHelper.TABLE_FREASON +" WHERE "+dbHelper.FREASON_NAME+"='"+name+"'";
		@SuppressWarnings("static-access")
		String selectQuery = "SELECT SUM(Amt) as totamt FROM FDAMDET WHERE refno='"+refno+"'";
		Cursor cursor = null;
		cursor = dB.rawQuery(selectQuery, null);
		
		while(cursor.moveToNext()){
			if (cursor.getString(cursor.getColumnIndex("totamt"))!=null)
				return cursor.getString(cursor.getColumnIndex("totamt"));
			else
				return "0.00";
			
		}
		
		return "0.00";
	}
	
}
